// pages/order/order.js
var util = require('../../utils/request.js');
var globalData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderStatus: [
      {
        content: '预约中'
      },
      {
        content: '停车中'
      },
      {
        content: '待支付'
      },
      {
        content: '已完成'
      }
    ],
    cardInfo: [],

    orderStatusCheck: 0,
    showTip: true,
    pageNumber: 1,
    pageSize: 10
  },
  activeStatus: function (e) {
    console.log(e.currentTarget.id);
    this.setData({
      orderStatusCheck: e.currentTarget.id,
      pageNumber: 1,
      pageSize: 10,
      hasMoreData: true,
      cardInfo: []
    })
    if (e.currentTarget.id == 2) {
      this.setData({
        showTip: false
      })
      this.unPay();
    } else if (e.currentTarget.id == 0) {
      this.appointment();
    } else if (e.currentTarget.id == 1) {
      this.using();
    } else if (e.currentTarget.id == 3) {
      this.finish();
    }
  },
  // 去支付
  goPay: function (e) {
    var sn = e.currentTarget.id;
    wx.navigateTo({
      url: '../payOrder/payOrder?sn=' + sn
    })
  },
  // 获取预约中订单
  appointment() {
    var that = this;
    var pageNumber = this.data.pageNumber;
    var pageSize = this.data.pageSize;
    util.req('/order/list', { 'memberId': globalData.memberId, 'orderStatus': 'appointment', 'pageNumber': pageNumber, 'pageSize': pageSize }, function (res) {
      console.log(res);
      if (res.resultCode == 200) {
        res.resultContent.map(function (item) {
          item.useDate = util.formatTime(new Date(item.useDate));
        })
        that.setData({
          cardInfo: res.resultContent
        })
      } else {
        wx.showToast({
          title: res.resultMsg,
          icon: 'none'
        })
      }
    })
  },
  // 获取正在停车的订单
  using() {
    var that = this;
    var pageNumber = this.data.pageNumber;
    var pageSize = this.data.pageSize;
    util.req('/order/list', { 'memberId': globalData.memberId, 'orderStatus': 'use', 'pageNumber': pageNumber, 'pageSize': pageSize }, function (res) {
      console.log(res);
      if (res.resultCode == 200) {
        res.resultContent.map(function (item) {
          item.useDate = util.formatTime(new Date(item.useDate));
        })
        that.setData({
          cardInfo: res.resultContent
        })
      } else {
        wx.showToast({
          title: res.resultMsg,
          icon: 'none'
        })
      }
    })
  },
  // 获取待支付的订单
  unPay() {
    var that = this;
    var pageNumber = this.data.pageNumber;
    var pageSize = this.data.pageSize;
    util.req('/order/list', { 'memberId': globalData.memberId, 'orderStatus': 'finish', 'pageNumber': pageNumber, 'pageSize': pageSize }, function (res) {
      console.log(res);
      if(res.resultCode == 200) {
        res.resultContent.map(function (item) {
          item.duration = util.formatSeconds(item.duration);
          item.useDate = util.formatTime(new Date(item.useDate));
          item.finishDate = util.formatTime(new Date(item.finishDate));
        })
        that.setData({
          cardInfo: res.resultContent
        })
      } else {
        wx.showToast({
          title: res.resultMsg,
          icon: 'none'
        })
      }
    })
  },
  // 获取已完成的订单
  finish() {
    var that = this;
    var pageNumber = this.data.pageNumber;
    var pageSize = this.data.pageSize;
    var cardInfo = this.data.cardInfo;
    util.req('/order/list', { 'memberId': globalData.memberId, 'orderStatus': 'pay', 'pageNumber': pageNumber, 'pageSize': pageSize }, function (res) {
      console.log(res);
      wx.hideLoading();
      if (res.resultCode == 200) {
        res.resultContent.map(function (item) {
          item.duration = util.formatSeconds(item.duration);
          item.useDate = util.formatTime(new Date(item.useDate));
        })
        if (res.resultContent.length < pageSize) {
          that.setData({
            cardInfo: cardInfo.concat(res.resultContent),
            hasMoreData: false
          })
        } else {
          that.setData({
            cardInfo: cardInfo.concat(res.resultContent),
            hasMoreData: true,
            pageNumber: pageNumber + 1
          })
        }
      } else {
        wx.showToast({
          title: res.resultMsg,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    options.to = options.to || 0;
    console.log(options.to);
    if(options.to == 0) {
      this.appointment();
    } else if(options.to == 2) {
      this.setData({
        orderStatusCheck: options.to
      })
      this.unPay();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    var that = this;
    if (that.data.orderStatusCheck == 3) {
      if (that.data.hasMoreData) {
        wx.showLoading({
          title: '正在加载...',
        })
        this.finish();
      } else {
        wx.showToast({
          title: '没有更多数据!',
          icon: 'none',
          duration: 800
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})